<?php
namespace AuctionCore\CarDealer;

use AuctionCore\System\ListBase;
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* "车商帐号"列表
* @author       爱民
*/
class DealerUserList extends ListBase
{

	private $base_sql = null;
	private $count_sql = null;

	public function __construct() {
		$query_sql = "SELECT d.dealer_id,d.contact_person,d.contact_mobile,u.uid,u.user_name,u.status,e.real_name FROM (SELECT * ";
    	$sql = " FROM au_car_dealer ";
		$this->base_sql = $query_sql.$sql." #condition# #order_by# #limit#) d 
                                            LEFT JOIN au_dealer_user du ON d.dealer_id=du.dealer_id 
                                            LEFT JOIN au_user u ON du.uid=u.uid 
                                            LEFT JOIN au_dealer_manager dm ON d.dealer_id = dm.dealer_id 
                                            LEFT JOIN au_employee e ON dm.emp_id = e.emp_id" ;
		$this->count_sql = "select count(*) count ".$sql ;
    }

    
    public function getList($page, $limit, $params){
        $page = (int)$page<1 ? 1 : (int)$page;
        $limit = (int)$limit<1 ? 10 : (int)$limit;
        //搜索 
        $map = array();
        //账号
        if(!empty($params['user_name'])){
            $map['u.user_name'] = array('eq',$params['user_name']);
        }
        //联系人
        if(!empty($params['contact_person'])){
            $map['cd.contact_person'] = array('like',$params['contact_person']);
        }
        //联系人电话
        if(!empty($params['contact_mobile'])){
            $map['cd.contact_mobile'] = array('eq',$params['contact_mobile']);
        }
        //账户类型
        if($params['category']>0){
            $map['cd.category'] = array('eq',$params['category']);
        }
        //要查询的列
        $cols = "cd.dealer_id,cd.category,cd.contact_person,cd.contact_mobile,u.uid,u.user_name,u.status,u.reg_date,e.real_name";
        
        $prefix = C("DB_PREFIX");
        $total = M('car_dealer')->alias('cd')
            ->join("LEFT JOIN {$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("LEFT JOIN {$prefix}user as u ON du.uid=u.uid")
            ->join("LEFT JOIN {$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("LEFT JOIN {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->count();
        
        $list = M('car_dealer')->alias('cd')->field($cols)
            ->join("LEFT JOIN {$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("LEFT JOIN {$prefix}user as u ON du.uid=u.uid")
            ->join("LEFT JOIN {$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("LEFT JOIN {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->order("u.uid desc")->limit($limit)->page($page)->order("cd.dealer_id desc")->select();
        
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;        
        
    }

    /**
    * 获取列表结果
    * @param    int $ev_page_num    每页条数
    * @param    int $page    页码
    * @return   Object
    */
    public function get($ev_page_num, $page){
        if(!$this->order_by){
            $this->order_by = 'ORDER BY dealer_id ASC';
        }


    	$dealer_id = $this->dealer_id;
    	$Model = new \Think\Model();
    	
    	//拍单列表信息
    	$sql = $this->base_sql;
        $sql = str_replace('#order_by#', $this->order_by, $sql);
        $sql = str_replace('#condition#', $this->condition, $sql);
        $skip = ($page<2?0:$ev_page_num*($page-1)) ;
        $limit = " limit {$skip},{$ev_page_num}" ;
        $sql = str_replace('#limit#', $limit, $sql);
    	$sql .= ";";

        //echo $sql;exit;
    	$list = $Model->query($sql);
    	
    	//dump($list);exit;
    	return $list;
    }

    public function count(){
    	$Model = new \Think\Model();
    	$sql = $this->count_sql;
    	$sql .= " {$this->condition}";
    	$sql .= ";";
        //echo $sql;exit;       
    	$r = $Model->query($sql);
    	return $r[0]['count'];
    }

}

?>